$(function () {
    xsdtz_init();
})


function xsdtz_init() {
    //采购方公司加载
    $("[name='xsdtz_xsgs']").jgbmtree($("[name='xsdtz_xsbm']"), $("[name='xsdtz_xsr']"), '', '')
    //客户加载
    $("[name='xsdtz_kh']").qyjgcombobox(false);
    //工程加载
    $("[name='xsdtz_gc']").gccombobox();
    var options = getXsdtzGridOption();
    $('#xsdtz-grid').datagrid(options);
}

function xsdtz_search() {
    var zt = $("#xsdtz-toolbar input[name='xsdtz_zt']").val();
    var xsdh = $("#xsdtz-toolbar input[name='xsdtz_xsdh']").val();
    var kh = $("#xsdtz-toolbar input[name='xsdtz_kh']").val();
    var xsgs = $("#xsdtz-toolbar input[name='xsdtz_xsgs']").val();
    var xsbm = $("#xsdtz-toolbar input[name='xsdtz_xsbm']").val();
    var xsr = $("#xsdtz-toolbar input[name='xsdtz_xsr']").val();
    var gc = $("#xsdtz-toolbar input[name='xsdtz_gc']").val();
//    var kh = $("#xy-toolbar input[name='xy_kh']").val();
//    var spzt = $("#xy-toolbar input[name='xy_spzt']").val();
    var options = $.extend(getxsdtzGridOption(),
        {queryParams: {zt: zt, xsdh: xsdh, khId: kh, xsgsId: xsgs, xsbmId: xsbm, xsrId: xsr, gcId: gc}});
    $('#xsdtz-grid').datagrid(options);
}

var xsdtzhref;

function xsdtz_editItem(title, action) {
    var record = $("#xsdtz-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的记录!", "info");
        return;
    }
    var href = $.webPath() + "/xsgl/xsdgl/xsdgl_content?xsdId=" + record.id;
    xsdtzhref = $.webPath() + "/xsgl/xsdgl/" + action;
    xsdtz_showDialog(title, href);
}

//新增、修改企业法人弹出界面
function xsdtz_showDialog(title, href) {

    var button = [
        {text: '保存', handler: xsdtz_saveItem}
    ];

    xsdtz_win = $.window({
        title: title,
        iconCls: 'icon-add2',
        url: href,
        width: 800,
        height: 480,
        buttons: button
    });
}

function xsdtz_saveItem() {
    $("#xsdtz_mx").edatagrid('saveRow');
    $("#xsdtz_fy").edatagrid('saveRow');
    $('#xsdtzglForm').tabs('select', "销售信息");
    $("#xsdtzglForm").form('submit', {
        url: xsdtzhref,
        onSubmit: function (param) {
            var data = $('#xsdtz_mx').edatagrid("getData");
            var fydata = $('#xsdtz_fy').edatagrid("getData");
            if (!data.rows.length) {
                $('#xsdtzglForm').tabs('select', "销售明细");
                $.messager.alert("提示", "请填写销售明细!", "waring");
                return false;
            }
            if (!fydata.rows.length) {
                $('#xsdtzglForm').tabs('select', "费用明细");
                $.messager.alert("提示", "请填写销售费用!", "waring");
                return false;
            }
            var arrayPrefix = 'xsd.xsmxList';
            var fyPrefix = 'xsd.xsfyList';
            //物资计划明细
            var paramString = "{";
            //物资计划明细
            for (var i = 0; i < data.rows.length; i++) {
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].", ['jzfs.id', 'jzfs.z', 'hsj', 'bhsj', 'bz', 'sbsl', 'js']);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            //费用物资计划
            for (var i = 0; i < fydata.rows.length; i++) {
                {
                    var objectString = $.toJSONStringPrefix(fydata.rows[i], fyPrefix + "[" + i + "].");
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            alert(paramString);
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(xsdtz_win).window('close');
                $('#xsdtz-grid').datagrid('reload');
                $('#xsdtz_layout').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}


function getXsdtzGridOption() {
    var options = {
        url: $.webPath() + "/xsgl/xsdgl/xsdList",
        toolbar: "#xsdtz-toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function (target) {
            $("#xsdtztz-edit").css('display', 'none');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'yly' ? "已完结" : this.zt == 'yzz' ? "已终止" : this.zt == 'ytj' ? "已提交" : "已保存";
                }
            })
            return data;
        },
        onClickRow: function (rowIndex, rowData) {
            if (rowData.zt == '已保存') {
                $("#xsdtz-edit").css('display', '');
            } else {
                $("#xsdtz-edit").css('display', 'none');
            }
            if ($("#xsdtz_layout #xsdtz_id") && $("#xsdtz_layout #xsdtz_id").val() != rowData.id) {
                $('#xsdtz_layout').layout('remove', 'center');
                $('#xsdtz_layout').layout('add', {
                    region: 'center',
                    split: true,
                    href: $.webPath() + '/xsgl/xsdgl/xsdgl_detail?xsdId=' + rowData.id
                });
            }
        }, onDblClickCell: function (index, field, value) {
            if (field == "xsdh") {
                var record = $("#xsdtz-grid").datagrid("getSelected")
                if (record.zt == '已保存')
                    xsdtz_editItem('修改协议', 'edit_xsdtz');
            }
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号', width: 60},
            {field: 'fhsj', title: '发货日期', width: 30},
            {field: 'kh_qc', title: '客户名称', width: 50},
            {field: 'gc_mc', title: '工程名称', width: 50},
            {field: 'xssl', title: '销售数量', width: 25},
            {field: 'xsje', title: '销售金额', width: 25},
            {field: 'thck_mc', title: '提货仓库', width: 50},
            {field: 'fyzr_z', title: '配送类型', width: 25},
            {field: 'dsyfje', title: '配送费用', width: 25},
            {field: 'xsgs_qc', title: '销售公司', width: 50},
            {field: 'xsbm_qc', title: '销售部门', width: 50},
            {field: 'xsr_xm', title: '销售人', width: 20},
            {field: 'zt', title: '状态', width: 20}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}